package com.finance.logistics.anlyzier;


import com.finance.framework.lang.Privilege;
import com.finance.report.bean.pojo.LogisticsData;
import com.finance.system.bean.pojo.LogisticsColumnDef;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 配送时效要求解析类
 * Created by CBWL on 2017/4/27.
 */
@Component
public class ScheduleTypeHandler extends AbstractHandler {
    @Override
    @Resource(name="consigneeAddressHandler")
    public void setNextHandler(AbstractHandler nextHandler){
        super.setNextHandler(nextHandler);
    }

    @Override
    public void doHandler(LogisticsData logisticsData) {
        this.logger.info("待解析运单信息：{}", net.sf.json.JSONObject.fromObject(logisticsData).toString());
        Integer scheduleType = logisticsData.getExpressScheduleType();
        LogisticsColumnDef logisticsColumnDef = this.logisticsMappingService.getByClasz(this.getClass().getCanonicalName());
        this.logger.info("匹配映射类查询{} 解析类获取clazz{}",logisticsColumnDef, this.getClass().getCanonicalName());
        if(logisticsColumnDef == null){
            this.logger.info("日志追踪11：{}", logisticsData.getWaybillNum());
            this.logger.error("LogisticsColumnDef is null 未知的运单列映射[clazz]:{}[运单号]：{}", new String[]{this.getClass().getCanonicalName(),logisticsData.getWaybillNum()});
            updateScheduleType(logisticsData,0);
            try {
                this.updateDataVild(logisticsData, Privilege.B9);
                this.logger.info("日志追踪12：{}", logisticsData.getWaybillNum());
                this.getNextHandler().doHandler(logisticsData);
            } catch (Exception e) {
                 logger.error(e.getMessage(),e);
             }
        }else{
            int target = HandlerHelper.analyze(scheduleType,logisticsColumnDef);
            if(target == -1){
                this.logger.info("日志追踪13：{}", logisticsData.getWaybillNum());
                this.getNextHandler().doHandler(logisticsData);
            }else{
                updateScheduleType(logisticsData, target);
                try {
                    this.updateDataVild(logisticsData, Privilege.B9);
                    this.logger.info("日志追踪14：{} 下个节点的运单对象{}", new String[]{logisticsData.getWaybillNum(), logisticsData.toString()});
                    this.getNextHandler().doHandler(logisticsData);
                } catch (Exception e) {
                    logger.error(e.getMessage(),e);
               }
            }
        }
    }

    private void updateScheduleType(LogisticsData logisticsData, int target) {
        String tableName = this.logisticsDataService.getDataTableName(logisticsData.getSignTime());
        LogisticsData logisticsData2update = this.logisticsDataService.getByWaybillNum(tableName,logisticsData.getWaybillNum());
        logisticsData2update.setExpressScheduleType(target);
        try {
            this.logisticsDataService.update(logisticsData2update);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
    }
}
